﻿Imports BUS
Imports DTO
Imports System.ComponentModel

Public Class ucPhieuNhapHang
    Private menuButtonLuuThem As New DevExpress.XtraBars.BarButtonItem
    Private menuButtonLuuDong As New DevExpress.XtraBars.BarButtonItem
    Private dtHangHoa As DataTable
    Private dtKhoHang As DataTable
    Private dtDonVi As DataTable
    Private dtPhieuNhapNhapHang As DataTable
    Private dsNhanVien As DataTable
    Private dtNhaCungCap As DataTable
    Private dtThanhToan As List(Of HinhThucTTDTO)
    Private dtDKThanhToan As List(Of DieuKhoanTTDTO)
    Private Sub ucPhieuNhapHang_Load(sender As Object, e As EventArgs) Handles MyBase.Load


        btnLuuVaThem.Links.Add(menuButtonLuuThem)

        menuButtonLuuDong.Caption = "Lưu Và Thêm"
        menuButtonLuuDong.Name = "menuButtonLuuThem"
        btnLuuVaThem.Links.Add(menuButtonLuuDong)

        'Dim dt As DataTable
        dtPhieuNhapNhapHang = New DataTable()
        dtPhieuNhapNhapHang.Columns.Add("MaHangHoa")
        dtPhieuNhapNhapHang.Columns.Add("TenHangHoa")
        dtPhieuNhapNhapHang.Columns.Add("MaKhoHang")
        dtPhieuNhapNhapHang.Columns.Add("MaDonVi")
        dtPhieuNhapNhapHang.Columns.Add("SoLuong")
        dtPhieuNhapNhapHang.Columns.Add("DonGia")
        dtPhieuNhapNhapHang.Columns.Add("ThanhTien")
        dgvHangHoa.DataSource = dtPhieuNhapNhapHang
        dtHangHoa = HangHoaBUS.LoadclMaHang_tabMuaHang

        'dgvHangHoa.DataSource = dtHangHoa
        dgvMaHang.DataSource = dtHangHoa
        dgvMaHang.ValueMember = "MaHangHoa"
        dgvMaHang.DisplayMember = "MaHangHoa"
        'RepositoryItemLookUpEdit1.DataSource = HangHoaBUS.LoadBangHoangHoa
        gridColTenHan.DataSource = dtHangHoa
        gridColTenHan.ValueMember = "MaHangHoa"
        gridColTenHan.DisplayMember = "TenHangHoa"

        dtKhoHang = KhoHangBUS.LoadBangKhoHang()
        dgvKhoHang_clKhoHang.DataSource = dtKhoHang
        dgvKhoHang_clKhoHang.DisplayMember = "TenKhoHang"
        dgvKhoHang_clKhoHang.ValueMember = "MaKhoHang"

        dtDonVi = DonViBUS.LayDSDonVi()
        dgvDonVi.DataSource = dtDonVi
        dgvDonVi.DisplayMember = "TenDonVi"
        dgvDonVi.ValueMember = "MaDonVi"

        menuButtonLuuThem.Caption = "Lưu Và Thêm"
        menuButtonLuuThem.Name = "menuButtonLuuThem"


        dtNhaCungCap = NhaCungCapBUS.LayDSNhaCungCapCbx
        cbbTenNCC.Properties.DataSource = dtNhaCungCap
        cbbTenNCC.Properties.DisplayMember = "TenNhaCungCap"
        cbbTenNCC.Properties.ValueMember = "MaNhaCungCap"

        cbbMaNCC.Properties.DataSource = dtNhaCungCap
        cbbMaNCC.Properties.DisplayMember = "TenNhaCungCap"
        cbbMaNCC.Properties.ValueMember = "MaNhaCungCap"

        dsNhanVien = NhanVienBUS.LayDSNhanVienCBX()
        cbbNhanVien.Properties.DataSource = dsNhanVien
        cbbNhanVien.Properties.DisplayMember = "TenNhanVien"
        cbbNhanVien.Properties.ValueMember = "MaNhanVien"
        cbbNhanVien.EditValue = dsNhanVien(0)("MaNhanVien").ToString

        dtThanhToan = HinhThucTTBUS.layDsHinhThucTT()
        cbbHinhThucThanhToan.Properties.DataSource = dtThanhToan
        cbbHinhThucThanhToan.Properties.DisplayMember = "TenThanhToan"
        cbbHinhThucThanhToan.Properties.ValueMember = "MaThanhToan"
        cbbThanhToan.EditValue = dtThanhToan(0).MaThanhToan

        dtDKThanhToan = DieuKhoanTTBUS.layDSDieuKhoan()
        cbbDKThanhToan.Properties.DataSource = dtDKThanhToan
        cbbDKThanhToan.Properties.ValueMember = "MaDieuKhoan"
        cbbDKThanhToan.Properties.DisplayMember = "TenDieuKhoan"
        cbbDKThanhToan.EditValue = 0

        dtpNgay.EditValue = Date.Today
    End Sub

    Private Sub dgvPhieuNhap_CellValueChanging(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles dgvPhieuNhap.CellValueChanging
        If dgvPhieuNhap.GetRowCellValue(e.RowHandle, clMaHang) Is Nothing And dgvPhieuNhap.GetRowCellValue(e.RowHandle, clTenHang) Is Nothing Then
            dgvPhieuNhap.AddNewRow()
        Else
            For i = 0 To dtPhieuNhapNhapHang.Rows.Count - 1
                If dtPhieuNhapNhapHang.Rows(i)("MaHangHoa").ToString = e.Value.ToString Then
                    dgvPhieuNhap.ClearSelection()
                    dgvPhieuNhap.FocusedRowHandle = i
                    dgvPhieuNhap.FocusedColumn = clSoLuong
                    dgvPhieuNhap.SelectRow(i)
                    Return
                End If
            Next
        End If
        If e.Column.FieldName = clMaHang.FieldName Or e.Column.FieldName = clTenHang.FieldName Then
            Dim temp() As DataRow = dtHangHoa.Select(String.Format("MaHangHoa='{0}' OR TenHangHoa='{1}'", e.Value.ToString, e.Value.ToString))
            Dim temp_2() As DataRow = dtKhoHang.Select(String.Format("TenKhoHang= '{0}'", temp(0)(3)))
            Dim temp_3() As DataRow = dtDonVi.Select(String.Format("MaDonVi='{0}'", temp(0)("MaDonVi")))
            dgvPhieuNhap.SetRowCellValue(e.RowHandle, clMaHang, temp(0)(0))
            dgvPhieuNhap.SetRowCellValue(e.RowHandle, clTenHang, temp(0)(0))
            dgvPhieuNhap.SetRowCellValue(e.RowHandle, clKhoHang, temp_2(0)(0))
            dgvPhieuNhap.SetRowCellValue(e.RowHandle, clSoLuong, 1)
            dgvPhieuNhap.SetRowCellValue(e.RowHandle, clDonVi, temp_3(0)("MaDonVi"))
            dgvPhieuNhap.SetRowCellValue(e.RowHandle, clDonGia, temp(0)("GiaMua"))
            dgvPhieuNhap.SetRowCellValue(e.RowHandle, clThanhTien, 0)
        ElseIf e.Column.FieldName = clSoLuong.FieldName Then
            If dgvPhieuNhap.GetRowCellValue(e.RowHandle, clSoLuong) Is Nothing And dgvPhieuNhap.GetRowCellValue(e.RowHandle, clDonGia) Is Nothing Then
            Else
                Dim SoLuong As Integer = Integer.Parse(dgvPhieuNhap.GetRowCellValue(e.RowHandle, clSoLuong).ToString)
                Dim DonGia As Integer = Integer.Parse(dgvPhieuNhap.GetRowCellValue(e.RowHandle, clDonGia).ToString)
                dgvPhieuNhap.SetRowCellValue(e.RowHandle, clThanhTien, SoLuong * DonGia)
            End If
        End If
    End Sub

    Private Sub cbbTenNCC_Properties_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles cbbTenNCC.Properties.ButtonClick
        If e.Button.Tag.ToString = "ThemNCC" Then
            Dim frmThemNCC As New frmThemNhaCungCap
            frmThemNCC.ShowDialog()
        End If
    End Sub

    Private Sub btnTaoMoi_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnTaoMoi.ItemClick
        dtPhieuNhapNhapHang.Clear()
    End Sub

    Private Sub btnNapLai_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnNapLai.ItemClick
        cbbTenNCC.EditValue = Nothing
        cbbDKThanhToan.EditValue = Nothing
        cbbHinhThucThanhToan.EditValue = Nothing
        dtpHanThanhToan.EditValue = Date.Today.AddDays(7)
    End Sub

    Private Sub btnLuuVaThem_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnLuuVaThem.ItemClick
        Dim muaHangDTO As MuaHangDTO = New MuaHangDTO("", Date.Today, cbbNhanVien.EditValue.ToString, cbbTenNCC.EditValue.ToString, cbbDKThanhToan.EditValue.ToString, cbbHinhThucThanhToan.EditValue.ToString,
                                                DirectCast(dtpHanThanhToan.EditValue, Date), 10, 0.1, DirectCast(cbbThanhToan.EditValue, Double), txtGhiChu.Text)
        If MuaHangBUS.ThemPhieu(muaHangDTO) Then
            For Each dr As DataRow In dtPhieuNhapNhapHang.Rows
                Dim temp As MuaHangChiTietDTO = New MuaHangChiTietDTO(-1, "", DirectCast(dr("SoLuong"), Integer), DirectCast(dr("DonGia"), Integer))
                If MuaHangChiTietBUS.ThemChiTietDonHang(temp) Then
                    dtPhieuNhapNhapHang.Rows.Remove(dr)
                Else
                    MessageBox.Show("Đơn hàng thêm không thành công.!")
                    Return
                End If
            Next
            dtPhieuNhapNhapHang.Clear()
        Else
            MessageBox.Show("Đơn hàng thêm không thành công.!")
            Return
        End If

    End Sub
End Class

Public Class ChiTietMaHang
    Public Property MaHang As String
    Public Property TenHang As String
End Class
